package com.jf.hotelreservation.demos.service.impl;

import com.alibaba.fastjson2.JSON;
import com.jf.hotelreservation.demos.bean.Tags;
import com.jf.hotelreservation.demos.mapper.TagsMapper;
import com.jf.hotelreservation.demos.service.TagsService;
import com.jf.hotelreservation.demos.utils.DataUtils;
import com.jf.hotelreservation.demos.utils.PageBean;
import com.jf.hotelreservation.demos.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TagsServiceImpl implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public String addTags(Tags tags) {
        if (tagsMapper.addTags(tags) != 0){
            return JSON.toJSONString(R.success("添加成功"));
        }else {
            return JSON.toJSONString(R.error("添加失败"));
        }

    }

    @Override
    public String queryAll(Integer currentPage, Integer pageSize) {
        List<Tags> tagList = tagsMapper.queryAll(DataUtils.getStartRecord(currentPage,pageSize),pageSize);
        long count = tagsMapper.count();
        if (!tagList.isEmpty()){
            PageBean pageBean = new PageBean();
            pageBean.setRows(tagList);
            pageBean.setTotal(count);
            return JSON.toJSONString(R.success("查询成功",pageBean));
        }else {
            return JSON.toJSONString(R.error("当前无数据"));
        }
    }

    @Override
    public String queryByHotel() {
        List<Tags> tagsList = tagsMapper.queryByHotel();
        if (tagsList.isEmpty()){
            return JSON.toJSONString(R.error("查询失败"));
        }else {
            return JSON.toJSONString(R.success(tagsList));
        }
    }
}
